package com.auxgroup.smarthome.inner.service;

import com.auxgroup.bridge.app.inner.dto.DeviceRepairDto;
import com.auxgroup.bridge.app.inner.vo.DeviceRepairVo;
import com.auxgroup.smarthome.BeanUtils;
import com.auxgroup.smarthome.appconst.BusinessConstant;
import com.auxgroup.smarthome.inner.entity.DeviceRepairEntity;
import com.auxgroup.smarthome.inner.repo.DeviceRepairRepo;
import com.auxgroup.smarthome.syscode.BusinessCode;
import com.auxgroup.smarthome.syscode.Syscode;
import com.auxgroup.smarthome.utils.PageContent;
import com.auxgroup.smarthome.utils.PageUtils;
import com.auxgroup.smarthome.web.ApiResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * 一键报修服务类
 * Created by lixiaoxiao on 17-8-7.
 */
@Service
public class DeviceRepairService {

    @Autowired
    private DeviceRepairRepo deviceRepairRepo;

    /**
     * 上报一键报修数据
     * 同一个设备相同故障未解决的只有一条
     * @param deviceRepairDto
     * @return
     */
    public ApiResponse addDeviceFault(DeviceRepairDto deviceRepairDto) {
        DeviceRepairEntity deviceRepairEntity = new DeviceRepairEntity();
        BeanUtils.copyAttrs(deviceRepairEntity, deviceRepairDto);
        DeviceRepairEntity deviceRepairEntityExist =  deviceRepairRepo.findByMacAndFaultTypeAndFaultStatus(
                deviceRepairEntity.getMac(),deviceRepairEntity.getFaultType(), BusinessConstant.FAULT_NOT_RESOLVED.getValue());
        if(deviceRepairEntityExist != null){
            return ApiResponse.prompt(BusinessCode.DEVICE_REPAIR_EXIST);
        }
        deviceRepairRepo.save(deviceRepairEntity);
        return ApiResponse.prompt(Syscode.SC_OK);
    }

    /**
     * 查询一键报修数据
     *
     * @param keyword
     * @param page
     * @param size
     * @return
     */
    public PageContent<DeviceRepairVo> findDeviceRepairList(String keyword, int page, int size) {
        Sort sort = new Sort(Sort.Direction.DESC, "createdAt");
        Pageable pageable = new PageRequest(page, size, sort);
        Page<DeviceRepairEntity> deviceRepairEntities = deviceRepairRepo.findAll(getWhereClause(keyword), pageable);
        Page<DeviceRepairVo> deviceFaultList = new PageUtils(DeviceRepairVo.class, deviceRepairEntities).buildVosPage();
        return new PageContent<DeviceRepairVo>(deviceFaultList);
    }

    /**
     * 配置动态查询条件
     *
     * @return
     */
    public Specification<DeviceRepairEntity> getWhereClause(String keyword) {
        return new Specification<DeviceRepairEntity>() {
            @Override
            public Predicate toPredicate(Root<DeviceRepairEntity> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();

                if (StringUtils.isNotBlank(keyword)) {
                    String inKeyword = "%" + keyword + "%";
                    Predicate predicate = cb.or(cb.like(root.get("repairId").as(String.class), inKeyword),
                            cb.like(root.get("mac").as(String.class), inKeyword),
                            cb.like(root.get("deviceSn").as(String.class), inKeyword),
                            cb.like(root.get("userName").as(String.class), inKeyword),
                            cb.like(root.get("userPhone").as(String.class), inKeyword));
                    list.add(predicate);
                }
                //query.groupBy(root.get("pkg")).orderBy(cb.desc(root.get("createTime")));
                return cb.and(list.toArray(new Predicate[list.size()]));
            }
        };
    }
}
